<?php

namespace app\admin\controller\merchant;

class Product extends Base
{
    protected $model = null;

    protected $searchFields = 'name';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = loader('admin')->loadModel('tysj\\Product');
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $model = $this->model;
            $model->append(['product_classify']);
            $model->alias('P');
            $model->join('store S', 'P.sid = S.id');
            $model->where($where);
            $model->order($sort, $order);
            $model->field('P.*,S.name as store_name');
            $list = $model->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        $productClassifyOptions = loadEnum('ProductClassify')->get_options();
        $productStatusOptions = loadEnum('ProductStatus')->get_options();
        $productExamineOptions = loadEnum('ProductExamine')->get_options();
        $this->assign('productClassifyOptions', (object)$productClassifyOptions);
        $this->assign('productStatusOptions', (object)$productStatusOptions);
        $this->assign('productExamineOptions', (object)$productExamineOptions);
        return $this->view->fetch();
    }

    public function odr()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }

    public function odr_edit() {
        $id = $this->request->get('id');
        if ($this->request->isAjax()) {
            $data = $this->request->post('row/a');
            $this->model->save($data, ['id' => $id]);
            $this->success('保存成功');
        }
        $row = $this->model->find($id);
        $this->assign('row', $row);
        return $this->view->fetch();
    }

    /*
     *  产品详情
     */
    public function info() {
        $id = $this->request->get('id');
        $row = $this->model->find($id);
        $row->append(['specs_list']);
        $this->assign('row', $row);

        $crowdOptions = loadEnum('Crowd')->get_options();
        $this->assign('crowdOptions', $crowdOptions);
        return $this->view->fetch();
    }

    /*
     *  通过审核
     */
    public function approve() {
        $id = $this->request->get('id');
        $data = $this->model->find($id);
        if ($this->request->isAjax()) {
            $loader = loader('admin');
            $post = $this->request->post();
            $validate = $loader->loadValidate('Product');
            if(!$validate->scene('approve')->check($post)) {
                $this->error($validate->getError());
            }
            $model = $loader->loadModel('tysj\\Product');
            $examineEnum = loadEnum('ProductExamine');
            $log = $loader->loadModel('tysj\\ProductExamineLog');
            $complete = $examineEnum->complete();
            try {
                $model->startTrans();
                $data = $model->find($post['id']);
                $model->where(['id' => $post['id']])->update(['examine' => $complete, $model->getModelAttr('updateTime') => time(), 'status' => $data['examine_after_status']]);
                $log->save([
                    'pid'      => $post['id'],
                    'admin_id' => $this->auth->id,
                    'before'   => $data['examine'],
                    'to'       => $complete,
                    'remark'   => '审核通过'
                ]);
                $model->commit();
            } catch (\Exception $e) {
                $model->rollback();
                $this->error($e->getMessage());
            }
            $this->success('保存成功');
        }
        $this->assign('data', $data);
        return $this->view->fetch();
    }

    /*
     * 拒绝
     */
    public function refuse() {
        $id = $this->request->get('id');
        $data = $this->model->find($id);
        if ($this->request->isAjax()) {
            $loader = loader('admin');
            $post = $this->request->post();
            $validate = $loader->loadValidate('Product');
            if(!$validate->scene('refuse')->check($post)) {
                $this->error($validate->getError());
            }
            $model = $loader->loadModel('tysj\\Product');
            $examineEnum = loadEnum('ProductExamine');
            $log = $loader->loadModel('tysj\\ProductExamineLog');
            $refuse = $examineEnum->refuse();
            try {
                $model->startTrans();
                $model->where(['id' => $post['id']])->update(['examine' => $refuse, $model->getModelAttr('updateTime') => time()]);
                $log->save([
                    'pid'      => $post['id'],
                    'admin_id' => $this->auth->id,
                    'before'   => $data['examine'],
                    'to'       => $refuse,
                    'remark'   => $post['remark']
                ]);
                $model->commit();
            } catch (\Exception $e) {
                $model->rollback();
                $this->error($e->getMessage());
            }
            $this->success('保存成功');
        }
        $this->assign('data', $data);
        return $this->view->fetch();
    }
}
